package com.ncloud.param.service.impl;

import java.util.List;

import com.ncloud.common.utils.RedisUtils;
import com.ncloud.param.service.CountyInfoService;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.param.entity.CountyInfo;
import com.ncloud.param.mapper.CountyInfoMapper;

@Service
public class CountyInfoServiceImpl implements CountyInfoService {
	
	@Autowired
	private CountyInfoMapper mapper;
	@Autowired
	private RedisUtils redisUtils;

	private static final Long expireTime= 7*24*60*60L;

	@Override
	public JSONArray getCountyInfoByLevel(JSONObject json) {
		if(!json.containsKey("level"))
			throw new UserException(ResultEnum.PARAM_ERROR);
		String level = json.getString("level");
		String key = "countyInfo_level_"+level;
		if(redisUtils.exists(key)){
			JSONArray info = redisUtils.getBean(key,JSONArray.class);
			return info;
		}else{
			List<CountyInfo> countyinfos = mapper.getCountyInfoByLevel(level);
			JSONArray array = JSON.parseArray(JSON.toJSONString(countyinfos));
			redisUtils.set(key,array,expireTime);
			return array;
		}
	}

	@Override
	public JSONArray getCountyInfoByParent(JSONObject json) {
		if(!json.containsKey("parentId"))
			throw new UserException(ResultEnum.PARAM_ERROR);
		String parentId = json.getString("parentId");
		String key = "countyInfo_"+parentId;
		if(redisUtils.exists(key)){
			JSONArray info = redisUtils.getBean(key,JSONArray.class);
			return info;
		}else{
			List<CountyInfo> countyinfos =  mapper.getCountyInfoByParent(parentId);
			JSONArray array = JSON.parseArray(JSON.toJSONString(countyinfos));
			redisUtils.set(key,array,expireTime);
			return array;
		}


	}

	@Override
	public JSONArray getProvinceInfoBySlaLevel(JSONObject json) {
		if(!json.containsKey("slaLevel"))
			throw new UserException(ResultEnum.PARAM_ERROR);
		String salLevel = json.getString("slaLevel");
		String key = "province_"+salLevel;
		if(redisUtils.exists(key)){
			JSONArray info = redisUtils.getBean(key,JSONArray.class);
			return info;
		}else{
			List<CountyInfo> countyinfos =  mapper.getProvinceInfoBySalLevel(salLevel);
			JSONArray array = JSON.parseArray(JSON.toJSONString(countyinfos));
			redisUtils.set(key,array,expireTime);
			return array;
		}
	}

	@Override
	public JSONArray getCityInfoBySlaLevel(JSONObject json) {
		if(!json.containsKey("slaLevel"))
			throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"slaLevel 没有数据！");
		if(!json.containsKey("provinceId"))
			throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"provinceId 没有数据！");
		String salLevel = json.getString("slaLevel");
		String provinceId = json.getString("provinceId");
		String key = "city_"+salLevel+"_"+provinceId;
		if(redisUtils.exists(key)){
			JSONArray info = redisUtils.getBean(key,JSONArray.class);
			return info;
		}else{
			List<CountyInfo> citys =  mapper.getCityInfoBySalLevel(salLevel,provinceId);
			JSONArray array = JSON.parseArray(JSON.toJSONString(citys));
			redisUtils.set(key,array,expireTime);
			return array;
		}
	}
	@Override
	public JSONObject getCountyByID(JSONObject json) {
		if(!json.containsKey("countyId"))
			throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"countyId 没有数据！");
		String countyId = json.getString("countyId");
		String key = "county_"+countyId;
		if(redisUtils.exists(key)){
			JSONObject info = redisUtils.getBean(key,JSONObject.class);
			return info;
		}else{
			CountyInfo countyInfo =  mapper.getCountyByID(countyId);
			JSONObject obj = JSON.parseObject(JSON.toJSONString(countyInfo));
			redisUtils.set(key,obj,expireTime);
			return obj;
		}
	}
}
